package com.pine.admin.config;

import com.pine.admin.modules.system.service.RedisService;
import com.pine.admin.modules.system.service.RedisService;
import com.pine.common.exception.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: Pine
 * @Date: 2018/5/28 下午7:07
 * @Email:771190883@qq.com
 */
@Slf4j
public class InterceptorConfig implements HandlerInterceptor {

    @Autowired
    private RedisService redisService;


    private long id;
    private static String methodName;
    private static String memberId;
    private static String accessToken;
    private static String secret;
    private static String createdTime;
    private static String clientIp;
    private static String clientType;
    private static String eCode;
    private static String uCode;
    private static String sign;
    private static String zpUserId;
    /**
     * 进入controller层之前拦截请求
     *
     * @param httpServletRequest
     * @param httpServletResponse
     * @param o
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {


        String url = httpServletRequest.getRequestURI();

        log.info("---------------------" + url + "开始进入请求地址拦截验证token----------------------------");




        accessToken = httpServletRequest.getHeader("accessToken");

        if (accessToken .equals("uuidnwpl2011") ){
            return true;
        }else {
            throw new ApiException("10001", "非法用户");
        }

//        zpUserId =  httpServletRequest.getHeader("zpUserId");
//
//        Object cToken = (Object) redisService.get("uidToken"+zpUserId);
//
//        String c = (String) cToken ;
//
//        if (accessToken .equals(c) ){
//            return true;
//        }else {
//            throw new ApiException("10001", "非法用户");
//        }
//        if (memberId == null) {
//            throw new ApiException("10001", "缺少业务ID参数");
//        }
//
//        if (methodName == null) {
//            throw new ApiException("10001", "缺少Api方法名参数");
//        }
//
//        if (secret == null) {
//            throw new ApiException("10001", "缺少秘钥参数");
//        }
//
//        if (createdTime == null) {
//            throw new ApiException("10001", "缺少提交时间参数");
//        }
//
//        if (clientIp == null) {
//            throw new ApiException("10001", "缺少客户端IP参数");
//        }
//
//        if (uCode == null) {
//            throw new ApiException("10001", "缺少设备码参数");
//        }
//        String signs = MD5Util.getMD5(secret + methodName + accessToken + createdTime + secret);
//
//        if (!sign.toUpperCase().equals(sign)) {
//            throw new ApiException("10002", "签名验证失败");
//        }
//
//        // 时间验证
//        if (Math.abs(Long.valueOf(createdTime) - System.currentTimeMillis()) > 10 * 60 * 1000) {
//            throw new ApiException("10003", "签名验证失败");
//        }

    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        log.info("--------------处理请求完成后视图渲染之前的处理操作---------------");
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        log.info("---------------视图渲染之后的操作-------------------------0");
    }
}
